package com.shj.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * @author songhuajian
 * @Description: TODO Jackson工具类
 * @date 2020/6/6 14:59
 **/
public class JsonUtil {

    /**
     * @Description json转对象
     * @author songhuajian
     * @date 2020/6/6 16:34
     */
    public static Object jsonToObj(Object obj,String jsonStr) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        return obj = mapper.readValue(jsonStr, obj.getClass());
    }

    /**
     * @Description 对象转json
     * @author songhuajian
     * @date 2020/6/6 16:34
     */
    public static String objToJson(Object obj) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(obj);
    }
}
